.TH std::ranges::views::lazy_split,std::ranges::lazy_split_view 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::ranges::views::lazy_split,std::ranges::lazy_split_view \- std::ranges::views::lazy_split,std::ranges::lazy_split_view

.SH Synopsis
   Defined in header <ranges>
   template< ranges::input_range V, ranges::forward_range Pattern >

   requires ranges::view<V> &&
            ranges::view<Pattern> &&
            std::indirectly_comparable<ranges::iterator_t<V>,
                                       ranges::iterator_t<Pattern>,  \fB(1)\fP \fI(since C++20)\fP
                                       ranges::equal_to> &&
            (ranges::forward_range<V> || /*tiny-range*/<Pattern>)
   class lazy_split_view

       : public ranges::view_interface<lazy_split_view<V, Pattern>>
   namespace views {

       inline constexpr /* unspecified */ lazy_split = /*            \fB(2)\fP \fI(since C++20)\fP
   unspecified */;

   }
   Call signature
   template< ranges::viewable_range R, class Pattern >

       requires /* see below */                                          \fI(since C++20)\fP

   constexpr ranges::view auto lazy_split( R&& r, Pattern&& pattern
   );
   template< class Pattern >
   constexpr /* range adaptor closure */ lazy_split( Pattern&&           \fI(since C++20)\fP
   pattern );
   Helper concepts
   template< class R >

   concept /*tiny-range*/ =                                              (exposition
       ranges::sized_range<R> &&                                     \fB(3)\fP only*)
       requires { /* is-statically-constexpr-sized */<R>; } &&

       (std::remove_reference_t<R>::size() <= 1);

   1) lazy_split_view takes a view and a delimiter, and splits the view into subranges
   on the delimiter.

   Two major scenarios are supported:

     * The view is an input_range, the delimiter is a single element (wrapped in a
       single_view).
     * The view is a forward_range, the delimiter is a view of elements.
   2) A RangeAdaptorObject. The expression views::lazy_split(e, f) is
   expression-equivalent to lazy_split_view(e, f).
   3) The exposition-only concept /*tiny-range*/<Pattern> is satisfied if Pattern
   satisfies sized_range, Pattern::size() is a constant expression and suitable as a
   template non-type argument, and the value of Pattern::size() is less than or equal
   to 1. Notably, empty_view and single_view satisfy this concept.

   lazy_split_view models the concepts forward_range and input_range when the
   underlying view V models respective concepts, and models common_range when V models
   both forward_range and common_range.

   The inner range (ranges::range_reference_t<lazy_split_view>) models the concepts
   forward_range and input_range when the underlying view V models respective concepts.
   It does not model common_range, and cannot be used with algorithms that expect a
   bidirectional_range or higher.

.SH Member functions

   constructor   constructs a lazy_split_view
   (C++20)       \fI(public member function)\fP
   base          returns a copy of the underlying (adapted) view
   (C++20)       \fI(public member function)\fP
   begin         returns an iterator to the beginning
   (C++20)       \fI(public member function)\fP
   end           returns an iterator or a sentinel to the end
   (C++20)       \fI(public member function)\fP
         Inherited from std::ranges::view_interface
   empty         returns whether the derived view is empty. Provided if it satisfies
   (C++20)       sized_range or forward_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   cbegin        returns a constant iterator to the beginning of the range.
   (C++23)       \fI(public member function of std::ranges::view_interface<D>)\fP
   cend          returns a sentinel for the constant iterator of the range.
   (C++23)       \fI(public member function of std::ranges::view_interface<D>)\fP
   operator bool returns whether the derived view is not empty. Provided if
   (C++20)       ranges::empty is applicable to it.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   front         returns the first element in the derived view. Provided if it
   (C++20)       satisfies forward_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP

   Nested classes

   outer_iterator the iterator type
   (C++20)        (exposition-only member class template*)
   inner_iterator the iterator type of the inner range
   (C++20)        (exposition-only member class template*)

   Deduction guides

.SH Notes

   The name lazy_split_view is introduced by the post-C++20 defect report P2210R2. It
   has the same lazy mechanism as that of the old split_view before change.

.SH Example


// Run this code

 #include <algorithm>
 #include <iostream>
 #include <ranges>
 #include <string_view>

 auto print = [](auto const& view)
 {
     // `view` is of std::views::lazy_split_view::__outer_iterator::value_type

     for (std::cout << "{ "; const auto element : view)
         std::cout << element << ' ';
     std::cout << "} ";
 };

 int main()
 {
     constexpr static auto source = {0, 1, 0, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9};
     constexpr int delimiter{0};
     constexpr std::ranges::lazy_split_view outer_view{source, delimiter};
     std::cout << "splits[" << std::ranges::distance(outer_view) << "]:  ";
     for (auto const& inner_view: outer_view)
         print(inner_view);

     constexpr std::string_view hello{"Hello C++ 20 !"};
     std::cout << "\\n" "substrings: ";
     std::ranges::for_each(hello | std::views::lazy_split(' '), print);

     constexpr std::string_view text{"Hello-+-C++-+-20-+-!"};
     constexpr std::string_view delim{"-+-"};
     std::cout << "\\n" "substrings: ";
     std::ranges::for_each(text | std::views::lazy_split(delim), print);
 }

.SH Output:

 splits[5]:  { } { 1 } { 2 3 } { 4 5 6 } { 7 8 9 }
 substrings: { H e l l o } { C + + } { 2 0 } { ! }
 substrings: { H e l l o } { C + + } { 2 0 } { ! }

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

     DR    Applied to         Behavior as published               Correct behavior
   P2210R2 C++20      the old split_view was too lazy to be  moves its functionality to
                      easily used                            lazy_split_view

.SH See also

   ranges::split_view a view over the subranges obtained from splitting another view
   views::split       using a delimiter
   (C++20)            \fI(class template)\fP (range adaptor object)
   ranges::join_view  a view consisting of the sequence obtained from flattening a view
   views::join        of ranges
   (C++20)            \fI(class template)\fP (range adaptor object)
